package com.neusoft.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class CommonUtil {

	//静态方法直接拿类名用
	public static String getCurrentDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();//获取当前日期和时间
		return sdf.format(calendar.getTime());//通过调用 format() 方法将 calendar 对象表示的日期和时间转换为字符串，并返回该字符串。
	}

	public static int checkDate(String curDates,String oldDates, int period) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long diffInDays = 0;
		try {
			Date currentDate = sdf.parse(curDates);
			Date oldDate = sdf.parse(oldDates);
			// 计算时间差
			long diffInMillies = Math.abs(currentDate.getTime() - oldDate.getTime());
			diffInDays = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
			// 判断时间差是否大于period
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (diffInDays >= period) {
			return 0;
		} else {
			return (int) Math.ceil(period-diffInDays);
		}
	}
}
